/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sip;

import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author arianareyessantiago
 */
public class BolusInsulinRunnableNew implements Runnable{
    
   
    double insulinAmountPerSec = 0;
    InitSip initSip = new InitSip();
    BolusDisplayWindow display;
    Bolus bolus = new Bolus();
    public static volatile boolean running = true;
    Alarm alarm = new Alarm();
    CheckStatus checkStatus = new CheckStatus();
    Double bolusInsulinLeft;
    Double bolusInsulinInitial;
    Double bolusInsulin;
    //Boolean runFlag = true
    
    
    @Override
    public void run(){
        //while(!CheckStatus.bubbles){
        display = new BolusDisplayWindow();
        display.setVisible(true);
        bolusInsulinLeft = 0.00;
        bolusInsulinInitial = bolus.getBolusInsulin();
        bolusInsulin = bolusInsulinInitial;
        insulinAmountPerSec = roundFiveDecimals(bolusInsulin/ 10);
         
        //System.out.println("Total Insulin before Bolus: " + InitSip.totalInsulinAmount);
        
        while(running){
            
            //System.out.println("bolusInsulinpersec: " + insulinAmountPerSec);
            
            SipLogger.Instance().debug("BolusInsulinThread has started.");

            try{
                for(int i = 0; i < 10; i++){
                    if(running){
                        if(!checkStatus.checkBubbles()){
                            InitSip.totalInsulinAmount = roundFiveDecimals(InitSip.totalInsulinAmount - insulinAmountPerSec);
                            
                            bolusInsulin = roundFiveDecimals(bolusInsulin - insulinAmountPerSec);
                            //System.out.println("Total Insulin after second: " + i + ", Insulin now: " + InitSip.totalInsulinAmount);
                            display.bolusProgressBar.setValue(i+1);
                            
                            //System.out.println(i);
                            if(i == 9){
                                display.dispose();
                            }
                            Thread.sleep(1000);
                        }
                    }
                }
            }catch (InterruptedException e) { 
                SipLogger.Instance().debug("BolusInsulinThread has been interrupted");       
            } 
           initSip.writeCurrentValues();
           //alarm.setAlarm("Bolus of " + Bolus.bolusInsulin + " has  been inserted.");
           SipLogger.Instance().infoH("Bolus of " + Bolus.bolusInsulin + " units was inserted.");
           if(bolusInsulin <= 0.00010){
               Bolus.bolusInsulin = 0;
           }
           else{
               SipLogger.Instance().error("The Bolus was not giving completly. There was " + bolusInsulin + " left out of " + bolusInsulinInitial +" .");
               SipLogger.Instance().infoH("The Bolus was not giving completly. There was " + bolusInsulin + " left out of " + bolusInsulinInitial + " .");
               //alarm.setAlarm("Bolus was not given completly.\n Go to your History to see how much was left. ");
               Bolus.bolusInsulin = 0;
           }
           running = false;
           Thread.interrupted();
 
        }
            
        //}
        SipLogger.Instance().debug("BolusInsulinRunnable has stopped.");
        running = true;
    }

    public void terminate(){
        display.dispose();
        running = false;
        SipLogger.Instance().debug("BolusInsulinRunnable was terminated.");
    }
    public double roundFiveDecimals(double d){
        DecimalFormat fiveDForm = new DecimalFormat("#.#####");
        return Double.valueOf(fiveDForm.format(d));
    }
    
    

} 